package com.kara.woodAgent.web.ws;

import io.vertx.core.http.Http2Settings;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.tracing.TracingPolicy;
import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static io.vertx.core.http.HttpServerOptions.DEFAULT_ALPN_VERSIONS;
import static io.vertx.core.http.HttpServerOptions.DEFAULT_INITIAL_SETTINGS_MAX_CONCURRENT_STREAMS;

/**
 * @author tzy
 * @version 1.0
 * @project wood_agent
 * @description
 * @date 2025/6/4 17:23:28
 */
@Data
@Component
@ConfigurationProperties(prefix = "spring.http-server")
public class HttpServerProperties {


    private String wsPath = "/ws/channel";

    private boolean auth = true;

    private String host = "0.0.0.0";

    private int port = 8080;

    private boolean compressionSupported = false;

    private int compressionLevel = 6;

    private int maxWebSocketFrameSize = 65536;

    private int maxWebSocketMessageSize = 65536 * 4;

    private boolean handle100ContinueAutomatically = false;

    private int maxChunkSize = 8192;

    private int maxInitialLineLength = 4096;

    private int maxHeaderSize = 8192;

    private int maxFormAttributeSize = 8192;

    private int maxFormFields = 512;

    private int maxFormBufferedBytes = 2048;

    private Http2Settings initialSettings = new Http2Settings()
            .setMaxConcurrentStreams(DEFAULT_INITIAL_SETTINGS_MAX_CONCURRENT_STREAMS);

    private List<HttpVersion> alpnVersions = new ArrayList<>(DEFAULT_ALPN_VERSIONS);

    private boolean http2ClearTextEnabled = true;

    private int http2ConnectionWindowSize = -1;

    private boolean decompressionSupported = false;

    private boolean acceptUnmaskedFrames = false;

    private int decoderInitialBufferSize = 256;

    private boolean perFrameWebSocketCompressionSupported = true;

    private boolean perMessageWebSocketCompressionSupported = true;

    private int webSocketCompressionLevel = 6;

    private boolean webSocketAllowServerNoContext = false;

    private boolean webSocketPreferredClientNoContext = false;

    private int webSocketClosingTimeout = 30;

    private TracingPolicy tracingPolicy = TracingPolicy.ALWAYS;

    private boolean registerWebSocketWriteHandlers = false;

    private int http2RstFloodMaxRstFramePerWindow = 400;

    private int http2RstFloodWindowDuration = 60;

    private TimeUnit http2RstFloodWindowDurationTimeUnit = TimeUnit.SECONDS;


    public HttpServerOptions getHttpServerOption() {
        HttpServerOptions options = new HttpServerOptions();
        options.setHost(this.getHost());
        options.setPort(this.getPort());
        options.setCompressionSupported(this.isCompressionSupported());
        options.setDecompressionSupported(this.isDecompressionSupported());
        options.setCompressionLevel(this.getCompressionLevel());
        options.setMaxWebSocketFrameSize(this.getMaxWebSocketFrameSize());
        options.setMaxWebSocketMessageSize(this.getMaxWebSocketMessageSize());
        options.setHandle100ContinueAutomatically(this.isHandle100ContinueAutomatically());
        options.setMaxChunkSize(this.getMaxChunkSize());
        options.setMaxInitialLineLength(this.getMaxInitialLineLength());
        options.setMaxHeaderSize(this.getMaxHeaderSize());
        options.setMaxFormAttributeSize(this.getMaxFormAttributeSize());
        options.setInitialSettings(this.getInitialSettings());
        options.setAlpnVersions(this.getAlpnVersions());
        options.setHttp2ConnectionWindowSize(this.getHttp2ConnectionWindowSize());
        options.setDecoderInitialBufferSize(this.getDecoderInitialBufferSize());
        options.setPerFrameWebSocketCompressionSupported(this.isPerFrameWebSocketCompressionSupported());
        options.setPerMessageWebSocketCompressionSupported(this.isPerMessageWebSocketCompressionSupported());
        options.setWebSocketCompressionLevel(this.getWebSocketCompressionLevel());
        options.setWebSocketAllowServerNoContext(this.isWebSocketAllowServerNoContext());
        options.setWebSocketPreferredClientNoContext(this.isWebSocketPreferredClientNoContext());
        options.setWebSocketClosingTimeout(this.getWebSocketClosingTimeout());
        options.setTracingPolicy(this.getTracingPolicy());

        return options;
    }

}
